package com.tocean.app.service.framework;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tocean.app.controller.framework.request.QuestionSaveRequest;
import com.tocean.app.controller.framework.search.QuestionSearchDto;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.entity.framework.Question;
import com.tocean.app.mapper.framework.QuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Random;


@Slf4j
@Service
public class QuestionService {

    @Autowired
    private QuestionMapper mapper;

    public Question findById(String id) {
        Question _entity = mapper.selectById(id);
        return _entity;
    }

    public Question findOne() {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper();
        Random random = new Random();
        final int START = 0;
        final int END = 50;
        boolean flag=false;
        Question _entity = null;
        List<Question> questions = mapper.selectList(wrapper
                .eq(Question::getIsDel, Boolean.FALSE)
                .eq(Question::getIsShow, Boolean.TRUE).last("limit 50"));

        while(!flag){
            for (int i = 0; i < questions.size(); i++) {
                long lastId = random.nextInt(END - START + 1) + START;
                long nowid = Long.parseLong(questions.get(i).getId()) % 100;
                if (nowid == lastId) {
                    _entity = questions.get(i);
                    flag=true;
                    break;
                }
            }
        }

        return _entity;
    }


    public List<Question> cacheList() {
        return mapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getIsDel, Boolean.FALSE));
    }

    public BasePageResponse<Question> page(QuestionSearchDto dto, Integer page, Integer size) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper();
        if (null != dto.getIsShow()) {
            wrapper.eq(Question::getIsDel, dto.getIsDel());
        }
//        wrapper.eq(Question::getIsDel, Boolean.FALSE);
        if (null != dto.getIsShow()) {
            wrapper.eq(Question::getIsShow, dto.getIsShow());
        }

        Page<Question> p = mapper.selectPage(new Page(page, size), wrapper);

        return new BasePageResponse().setTotal(p.getTotal()).setData(p.getRecords());
    }

    public BasePageResponse<Question> showpage(QuestionSearchDto dto, Integer page, Integer size) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Question::getIsDel, Boolean.FALSE);
        wrapper.eq(Question::getIsShow, Boolean.TRUE);


        Page<Question> p = mapper.selectPage(new Page(page, size), wrapper);

        return new BasePageResponse().setTotal(p.getTotal()).setData(p.getRecords());
    }


    public void save(QuestionSaveRequest request) {
        Question _entity = new Question();
        _entity.setStem(request.getStem());
        _entity.setId(request.getId());
        _entity.setAnalysis(request.getAnalysis());
        _entity.setOptionA(request.getOptionA());
        _entity.setOptionB(request.getOptionB());
        _entity.setOptionC(request.getOptionC());
        _entity.setOptionD(request.getOptionD());
        _entity.setRightOption(request.getRightOption());
        _entity.setIsShow(request.getIsShow());
        mapper.insert(_entity);
    }


    public void update(String id, QuestionSaveRequest request) {
        Optional<Question> optional = Optional.ofNullable(findById(id));
        if (optional.isPresent()) {
            Question _entity = optional.get();
            _entity.setStem(request.getStem());
            _entity.setId(request.getId());
            _entity.setAnalysis(request.getAnalysis());
            _entity.setOptionA(request.getOptionA());
            _entity.setOptionB(request.getOptionB());
            _entity.setOptionC(request.getOptionC());
            _entity.setOptionD(request.getOptionD());
            _entity.setRightOption(request.getRightOption());
            _entity.setIsShow(request.getIsShow());
            mapper.updateById(_entity);
        }
    }


    public void handleIsDel(String id) {
        Optional<Question> optional = Optional.ofNullable(findById(id));
        if (optional.isPresent()) {
            Question _entity = optional.get();
            _entity.setIsDel(!_entity.getIsDel());
            mapper.updateById(_entity);
        }
    }

}
